<?
//include 'koneksi.php';

function autonumber($table="",$field="",$awal="")
{

$sql_select2 = "select $field from $table order by $field desc limit 1 ";
$result2 = mysql_query($sql_select2);
$temp2 = mysql_fetch_array($result2);

$sql_select1 = "SELECT CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE TABLE_NAME = '$table' limit 1"; 
$result1 = mysql_query($sql_select1);
$temp1 = mysql_fetch_array($result1);
   
   if($temp2[0] == "")
   {
      $nol = nol_digit($temp1[0]-1,$awal);
	  $hasil = $awal.$nol.'1';
	  }else{
	  $pnjg1 = strlen($awal);
      $pnjg2 = $temp1[0] - strlen($awal);
	  $nilai = substr($temp2[0],$pnjg1,$pnjg2);
	  $nilai = $nilai+1;

	  $hsl = nol_digit(($temp1[0])-strlen($nilai),$awal); 	  
      $hasil = $awal.$hsl.$nilai;
	  
    }	
   return $hasil;

}

function nol_digit($digit ="",$awal="")
{
    
	 
     $pjg_awal = strlen($awal);
     $dig = $digit-$pjg_awal;	 
     $nil=null; 
     for($i=0;$i<$dig;$i++)
     {
        $nil = $nil.'0';
	 }
    return $nil; 
}
     
?>